<template>
	<view class="content">
		<uni-nav-bar>
			<block slot="left">
				<view class="city" @click="goIndex">
					<!-- 返 回 -->
				</view>
			</block>
			<view class="input-view">
				<input confirm-type="search" class="nav-bar-input" type="text" placeholder="请输入物料编码" @confirm="confirm"
					id="scaninput" v-model="resInfo" autofocus="isFocus" :focus="focus" @blur="poinblur" />
			</view>
			<block slot="right">
				<view class="city"></view>
			</block>
		</uni-nav-bar>
		<view v-for="(item,index) in quertData">
			<uni-card :title="'产品名称:'+item.itemName">

				<!-- <text class="uni-body">{{ item[index].DocNo }}</text>-->
				<view>
					<!-- <text
						class="uni-body">单位：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ item.unitOfMeasure }}</text> -->
					<text class="uni-body" style="padding-left: 0rpx;">批次号：{{ item.batchNumber }}</text>
				</view>
				<view>
					<text class="uni-body">仓库编号：{{ item.warehouseCode }}</text>
					<!-- <text class="uni-body" style="padding-left: 80rpx;">仓库名称：{{ item.warehouseName }}</text> -->
				</view>
				<view>
					<text class="uni-body" style="width: 50%;">库区编号：{{ item.locationCode }}</text>
					<!-- <text class="uni-body"
						style="padding-left: 80rpx;width: 50%;">库区名称：{{ item.locationName }}</text>--><br> 
				</view>
				<view>
					<text class="uni-body" style="width: 50%;">库位编号：{{ item.areaCode }}</text>
					<!-- <text class="uni-body" style="padding-left: 80rpx;width: 50%;">库位名称：{{ item.areaName }}</text> --><br>
				</view>
				<view>
					<text class="uni-body" style="width: 50%;">库存数量：{{ item.qty }}</text>
					<!-- <text class="uni-body"
						style="padding-left: 80rpx;width: 50%;">库存保留数量：{{ item.quantityReserved }} --></text><br>
				</view>
			</uni-card>
		</view>
		<!-- <uni-section title="" type="line" v-for="(item,index) in quertData">
			
		</uni-section> -->
	</view>
</template>

<script>
	import {
		getToken
	} from '@/utils/auth'
	import common from '../../pages/common/common.js'
	const printer = uni.requireNativePlugin('LcPrinter');
	const modal = uni.requireNativePlugin('modal');
	var globalEvent = uni.requireNativePlugin('globalEvent');
	var main, receiver, filter;
	var ScanDeviceClass = plus.android.importClass("android.device.ScanDevice");
	var scanDevice;
	export default {
		data() {
			return {
				resInfo: '', //搜索框内容
				isFocus: true,
				focus: true,
				quertData: [{
					//产品编码
					itemCode: '',
					//产品名称
					itemName: '',
					//单位
					unitOfMeasure: '',
					//批次编号
					batchNumber: '',
					//仓库ID
					warehouseId: '',
					//仓库编号
					warehouseCode: '',
					//仓库名称
					warehouseName: '',
					//库区ID
					locationId: '',
					//库区编号
					locationCode: '',
					//库区名称
					locationName: '',
					//库位ID
					areaId: '',
					//库位编号
					areaCode: '',
					//库位名称
					areaName: '',
					//库存数量
					qty: '',
					//库存保留数量
					quantityReserved: '',
				}],
				baseUrl: getApp().globalData.config.baseUrl,
				userName: this.$store.state.user.name,
				orgId: getApp().globalData.config.orgId,
			}
		},

		onShow() {
			this.orgId = uni.getStorageSync('orgId');
			scanDevice = new ScanDeviceClass();
			scanDevice.openScan(); // 打开扫描
			scanDevice.setOutScanMode(0); // 扫描模式		
			this.initScan(this.productList);
			this.registerScan();
		},

		onHide: function() {
			this.unregisterScan();
		},

		destroyed: function() {
			this.unregisterScan();
		},

		onBackPress(options) {
			var that = this;
			if (options.from === 'navigateBack') {
				return false;
			}
			//that.unregisterScan();
			uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
				delta: 1
			});
			return true;
		},

		methods: {
			initScan(popFlag) {
				let _this = this;
				main = plus.android.runtimeMainActivity(); //获取activity  
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				filter = new IntentFilter();
				filter.addAction("scan.rcv.message"); // 换你的广播动作  
				receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						let code = intent.getByteArrayExtra('barocode');
						let codeStr = common.utf8ByteToUnicodeStr(code);
						scanDevice.stopScan(); // 停止扫描
						var result = common.scanCode(codeStr);
						if (result[0] == "P") {
							setTimeout(() => {
								_this.focus = true;
							}, 100)
						} else {
							uni.showToast({
								title: '请扫描物料编码',
								icon: 'error',
								duration: 2000
							});
							return;
						}
						_this.resInfo = result[1];
						_this.confirm(result[1]);
					}
				});
			},

			registerScan() {
				main.registerReceiver(receiver, filter);
			},

			unregisterScan() {
				main.unregisterReceiver(receiver);
			},

			//返回
			goIndex() {

			},

			//回车查询
			confirm() {
				this.quertData = [];
				let token = getToken();

				uni.request({
					url: this.baseUrl + '/system/view/list',
					data: {
						// itemId: this.resInfo,
						// orgId: this.orgId,
						// userName: this.userName,

						itemCode: this.resInfo,
						itemName: "",
						batchNumber: "",
						orgId: this.orgId,
						pageNum: 1,
						pageSize: 1000,
						warehouseCode: "",
						locationCode: "",
						areaCode: "",
					},
					method: 'GET',
					header: {
						'authorization': token
					},
					dataType: 'json',
					success: (res) => {
						if (res.data.code == '200') {
							console.log(res)
							this.quertData = res.data.rows;
						} else {
							uni.showToast({
								title: '查询失败',
								icon: 'none',
								duration: 2000
							});
							return;
						}
					}
				})


				// uni.request({
				// 	url:this.baseUrl+'/system/detail/detaillist',
				// 	data:{
				// 		itemId : this.resInfo,
				// 		orgId:this.orgId,
				// 		userName:this.userName
				// 	},
				// 	method: 'POST',
				// 	header:{
				// 		'authorization': token
				// 	},
				// 	dataType:'json',
				// 	success: (res) =>{
				// 		if(res.data.code == '200'){
				// 			this.quertData =  res.data.rows;
				// 		}else{
				// 			uni.showToast({
				// 				title: '查询失败',
				// 				icon: 'none',
				// 				duration: 2000
				// 			});
				// 			return;
				// 		}
				// 	}
				// })
				//this.quertData.push(temp);
			},

			poinblur() {

			},

			scan() {
				scanDevice.stopScan(); // 停止扫描
				scanDevice.startScan();
			},
		}
	}
</script>

<style>
	.content {
		/* display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center; */
		padding-bottom: 20rpx;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}

	$nav-height: 30px;

	.box-bg {
		background-color: #F5F5F5;
		padding: 5px 0;
	}

	.city {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: flex-start;
		/* // width: 160rpx; */
		margin-left: 4px;
		font-size: 26rpx;
	}

	.input-view {
		/* #ifndef APP-PLUS-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		/* // width: 500rpx; */
		flex: 1;
		background-color: #f8f8f8;
		height: $nav-height;
		border-radius: 15px;
		padding: 0 50px;
		flex-wrap: nowrap;
		margin: 5px 0;
		line-height: $nav-height;
		font-size: 26rpx;
	}

	.input-uni-icon {
		line-height: $nav-height;
	}

	.nav-bar-input {
		height: $nav-height;
		line-height: $nav-height;
		/* #ifdef APP-PLUS-NVUE */
		width: 370rpx;
		/* #endif */
		padding: 0 7px;
		margin-top: 5px;
		font-size: 16px;
		background-color: #f8f8f8;
	}
</style>